package com.ey12.db.dao;

import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ey12.db.domain.GmGameStore;
import com.ey12.db.domain.GmUser;

/**
 * Data access object (DAO) for domain model class GmGameStore.
 * 
 * @see com.ey12.db.domain.GmGameStore
 * @author MyEclipse Persistence Tools
 */

public class GmGameStoreDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(GmGameStoreDAO.class);
	// property constants
	public static final String GOODS_TYPE = "goodsType";
	public static final String STORE_NUM = "storeNum";
	public static final String UPDATE_DATE = "updateDate";
	public static final String TITLE = "title";
	public static final String INTRO = "intro";
	public static final String PRICE = "price";
	public static final String VALID_DATE = "validDate";
	public static final String IS_ONLINE = "isOnline";
	public static final String REMARK = "remark";
	public static final String IS_QUICK = "isQuick";
	public static final String IS_DEPOSIT = "isDeposit";

	protected void initDao() {
		// do nothing
	}

	public void saveOrUpdate(GmGameStore transientInstance) {
		log.debug("saving GmGameStore instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			re.printStackTrace();
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(GmGameStore persistentInstance) {
		log.debug("deleting GmGameStore instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public GmGameStore findById(java.lang.Integer id) {
		log.debug("getting GmGameStore instance with id: " + id);
		try {
			GmGameStore instance = (GmGameStore) getHibernateTemplate().get(
					"com.ey12.db.domain.GmGameStore", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(GmGameStore instance) {
		log.debug("finding GmGameStore instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
	
	public List defineGameStore(GmGameStore gm){
		try {
			String queryString = " from GmGameStore g where g.isDel=1 and g.id="+gm.getId();
			System.out.println(queryString);
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			re.printStackTrace();
			throw re;
		}
	}
//	public List defineGameStore(GmGameStore gm){
//		try {
//			String queryString = " from GmGameStore g where  g.isDel=1 and g.gmUser.userName='"+gm.getGmUser().getUserName()+"' and g.gmGame.gameName='"+gm.getGmGame().getGameName()
//			+"' and g.gmCserver.cserverName='"+gm.getGmCserver().getCserverName()+"' and g.gmCserver.severType='"+gm.getGmCserver().getSeverType()+"' and g.gmCserver.unit='"+gm.getGmCserver().getUnit()
//			+"' and g.gmCserver.camp='"+gm.getGmCserver().getCamp()+"' and g.gmCserver.gmGame.gameName='"+gm.getGmGame().getGameName()+"'";
//			System.out.println(queryString);
//			return getHibernateTemplate().find(queryString);
//		} catch (RuntimeException re) {
//			log.error("find all failed", re);
//			re.printStackTrace();
//			throw re;
//		}
//	}

	public List findMyGoodDetial(GmGameStore gm){
		try {
			String queryString = " from GmGameStore g where g.isDel=1 and g.gmUser.userName='"+gm.getGmUser().getUserName()+"'";
			if(gm.getGmGame().getGameName()!=null&&gm.getGmGame().getGameName()!=""){
				queryString+=" and g.gmGame.gameName='"+gm.getGmGame().getGameName()+"'";
			}
			if(gm.getGmCserver().getCserverName()!=null&&gm.getGmCserver().getCserverName()!=""){
				queryString+=" and g.gmCserver.cserverName='"+gm.getGmCserver().getCserverName()+"'";
			}
			if(gm.getGmCserver().getSeverType()!=null&&gm.getGmCserver().getSeverType()!=""){
				queryString+=" and g.gmCserver.severType like '%"+gm.getGmCserver().getSeverType()+"%'"; 
			}
			if(gm.getGmCserver().getUnit()!=null&&gm.getGmCserver().getUnit()!=""){
				queryString+=" and g.gmCserver.unit='"+gm.getGmCserver().getUnit()+"'";
			}
			if(gm.getGmCserver().getCamp()!=null&&gm.getGmCserver().getCamp()!="") {
				queryString+=" and g.gmCserver.camp='"+gm.getGmCserver().getCamp()+"'";
			}
			System.out.println(queryString);
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			re.printStackTrace();
			throw re;
		}
	}
	
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding GmGameStore instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from GmGameStore as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByGoodsType(Object goodsType) {
		return findByProperty(GOODS_TYPE, goodsType);
	}

	public List findByStoreNum(Object storeNum) {
		return findByProperty(STORE_NUM, storeNum);
	}

	public List findByUpdateDate(Object updateDate) {
		return findByProperty(UPDATE_DATE, updateDate);
	}

	public List findByTitle(Object title) {
		return findByProperty(TITLE, title);
	}

	public List findByIntro(Object intro) {
		return findByProperty(INTRO, intro);
	}

	public List findByPrice(Object price) {
		return findByProperty(PRICE, price);
	}

	public List findByValidDate(Object validDate) {
		return findByProperty(VALID_DATE, validDate);
	}

	public List findByIsOnline(Object isOnline) {
		return findByProperty(IS_ONLINE, isOnline);
	}

	public List findByRemark(Object remark) {
		return findByProperty(REMARK, remark);
	}

	public List findByIsQuick(Object isQuick) {
		return findByProperty(IS_QUICK, isQuick);
	}

	public List findByIsDeposit(Object isDeposit) {
		return findByProperty(IS_DEPOSIT, isDeposit);
	}

	public List findMembusinese(String gameName,String userName,String gameServer) {
		log.debug("finding all GmGameStore instances");
		try {
			String queryString = "from GmGameStore g where g.isDel=1 ";
			if(gameName!=null&&gameName!=""){
				queryString+=" and g.gmCserver.gmGame.gameName='"+gameName+"'";
			}
			if(userName!=null&&userName!=""){
				queryString+=" and g.gmUser.userName='"+userName+"'";
			}
			if(gameServer!=null&&gameServer!=""){
				queryString+=" and g.gmCserver.severType like '%"+gameServer+"%'";
			}
			System.out.println("findMembusinese="+queryString);
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			re.printStackTrace();			
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List findAll() {
		log.debug("finding all GmGameStore instances");
		try {
			String queryString = "from GmGameStore where isDel=1";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List findTheSame(GmGameStore gm){
		log.debug("finding all GmGameStore instances");
		try {
			String queryString = "from GmGameStore g where  g.isDel=1 and g.gmGame.gameName='"+gm.getGmGame().getGameName()+"' and g.gmUser.userName='"+gm.getGmUser().getUserName()+"' and g.gmCserver.cserverName='"+gm.getGmCserver().getCserverName()+"' and g.gmCserver.severType='"+gm.getGmCserver().getSeverType()+"' and g.gmCserver.unit='"+gm.getGmCserver().getUnit()+"' and g.gmCserver.camp='"+gm.getGmCserver().getCamp()+"'";
			System.out.println(queryString);
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			re.printStackTrace();
			throw re;
		}
	}
	public GmGameStore merge(GmGameStore detachedInstance) {
		log.debug("merging GmGameStore instance");
		try {
			GmGameStore result = (GmGameStore) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public List findGood(String userName){
		try {
			String queryString = " from GmGameStore g where g.gmUser.userName=? and g.isDel=1";//select g.gmGame.gameName, g.gmCserver.cserverName,  g.gmCserver.unit, g.price
			System.out.println(queryString);
			return getHibernateTemplate().find(queryString,userName);
		} catch (RuntimeException re) {
			re.printStackTrace();
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public void attachDirty(GmGameStore instance) {
		log.debug("attaching dirty GmGameStore instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(GmGameStore instance) {
		log.debug("attaching clean GmGameStore instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static GmGameStoreDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (GmGameStoreDAO) ctx.getBean("GmGameStoreDAO");
	}
	
	public List findGameDetialData(String gameName,String cserverName,String serverType) {
	try {
		String queryString = "from GmGameStore g where g.isDel=1 and g.isOnline=1 and g.gmUser.isOnline=1";
		if(gameName!=null&&gameName!=""){		
				queryString=queryString+" and g.gmGame.gameName='"+gameName+"'";									
		}
		if(cserverName!=null&&cserverName!=""){
				queryString=queryString+" and g.gmCserver.cserverName='"+cserverName+"'";								
		}
		if(serverType!=null&&serverType!=""){
				queryString=queryString+" and g.gmCserver.severType like '%"+serverType+"%'";								
		}
		//queryString+=queryString+" order by g.gmCserver.severType";
		System.out.println("findFundDetial--queryString="+queryString);
		return getHibernateTemplate().find(queryString);
	} catch (RuntimeException re) {
		re.printStackTrace();
		log.error("find all failed", re);
		throw re;
	}
}
	public List findLoginGameDetialData(String gameName,String cserverName,String serverType,String unit) {
		try {
			String queryString = "from GmGameStore g where g.isDel=1 and g.isOnline=1";
			if(gameName!=null&&gameName!=""){		
					queryString=queryString+" and g.gmGame.gameName='"+gameName+"'";									
			}
			if(cserverName!=null&&cserverName!=""){
					queryString=queryString+" and g.gmCserver.cserverName='"+cserverName+"'";								
			}
			if(serverType!=null&&serverType!=""){
					queryString=queryString+" and g.gmCserver.severType like '%"+serverType+"%'";								
			}
			if(unit!=null&&unit!=""){
				queryString=queryString+" and g.gmCserver.unit='"+unit+"'";								
			}
			//queryString+=queryString+" order by g.gmCserver.severType";
			System.out.println("findFundDetial--queryString="+queryString);
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			re.printStackTrace();
			log.error("find all failed", re);
			throw re;
		}
	}
	
	
	
}